
import config
import app.tools as tool
from PyQt4 import QtCore, QtGui, Qt
from app.ui import Icon
from app.tools.table import TableWidget, TableWidgetItem
from lib.redis import RedisFacotry
from lib.tools.type import RedisConnectType, ConnectTabIndex
from lib.storage import Storage



class ClientWidget(QtGui.QWidget):

    def __init__(self, main=None, parent=None):
        super(ClientWidget, self).__init__(parent)
        self.icon = Icon()
        self.main = main
        self.logger = main.logger
        self.parent = parent
        self.init_ui()

    def init_style(self):
        style = QtCore.QString(open(config.RESOURCE_STYLE_SHEET).read())
        self.setStyleSheet(style)

    def init_ui(self):
        parent_geometry = self.parent.geometry()
        current_width = parent_geometry.width()
        current_height = parent_geometry.height()

        self.vertical_layout_widget = QtGui.QWidget(self.parent)
        # self.vertical_layout_widget.setGeometry(QtCore.QRect(0, 0, 531, 321))
        parent_rect = QtCore.QRect(0, 0, current_width, current_height)
        self.vertical_layout_widget.setGeometry(parent_rect)
        self.vertical_layout_widget.setObjectName(tool._from_utf8("vertical_layout_widget"))

        self.vertical_layout = QtGui.QVBoxLayout(self.vertical_layout_widget)

        self.client_form_layout_widget = QtGui.QWidget(self.vertical_layout_widget)

        client_form_layout_widget_rect = QtCore.QRect(20, 20, current_width - (current_width * 0.1), current_height)
        self.client_form_layout_widget.setGeometry(client_form_layout_widget_rect)
        self.client_form_layout_widget.setObjectName(tool._from_utf8("client_form_layout_widget"))

        self.form_layout_client = QtGui.QFormLayout(self.client_form_layout_widget)
        self.form_layout_client.setObjectName(tool._from_utf8("form_layout_client"))

        self.client_name_label = QtGui.QLabel(self.client_form_layout_widget)
        self.client_name_label.setObjectName(tool._from_utf8("client_name_label"))
        self.client_name_label.setText(tool._from_utf8('Name: '))
        self.form_layout_client.setWidget(0, QtGui.QFormLayout.LabelRole, self.client_name_label)

        self.client_name_line_edit = QtGui.QLineEdit(self.client_form_layout_widget)
        self.client_name_line_edit.setObjectName(tool._from_utf8("client_name_line_edit"))
        self.form_layout_client.setWidget(0, QtGui.QFormLayout.FieldRole, self.client_name_line_edit)

        self.client_host_label = QtGui.QLabel(self.client_form_layout_widget)
        self.client_host_label.setObjectName(tool._from_utf8("client_host_label"))
        self.client_host_label.setText(tool._from_utf8('Host: '))
        self.form_layout_client.setWidget(1, QtGui.QFormLayout.LabelRole, self.client_host_label)

        self.client_host_line_edit = QtGui.QLineEdit(self.client_form_layout_widget)
        self.client_host_line_edit.setObjectName(tool._from_utf8("client_host_line_edit"))
        self.client_host_line_edit.setText(tool._from_utf8(config.REDIS_DEFAULT_HOST))
        self.form_layout_client.setWidget(1, QtGui.QFormLayout.FieldRole, self.client_host_line_edit)

        self.client_port_label = QtGui.QLabel(self.client_form_layout_widget)
        self.client_port_label.setObjectName(tool._from_utf8("client_port_label"))
        self.client_port_label.setText(tool._from_utf8('Port: '))
        self.form_layout_client.setWidget(2, QtGui.QFormLayout.LabelRole, self.client_port_label)

        self.client_port_line_edit = QtGui.QLineEdit(self.client_form_layout_widget)
        self.client_port_line_edit.setObjectName(tool._from_utf8("client_port_line_edit"))
        self.client_port_line_edit.setText(tool._from_utf8(config.REDIS_DEFAULT_PORT))
        self.form_layout_client.setWidget(2, QtGui.QFormLayout.FieldRole, self.client_port_line_edit)

        self.client_auth_label = QtGui.QLabel(self.client_form_layout_widget)
        self.client_auth_label.setObjectName(tool._from_utf8("client_auth_label"))
        self.client_auth_label.setText(tool._from_utf8('Auth: '))
        self.form_layout_client.setWidget(3, QtGui.QFormLayout.LabelRole, self.client_auth_label)

        self.client_auth_line_edit = QtGui.QLineEdit(self.client_form_layout_widget)
        self.client_auth_line_edit.setObjectName(tool._from_utf8("client_auth_line_edit"))
        self.client_auth_line_edit.setEchoMode(Qt.QLineEdit.Password)
        self.form_layout_client.setWidget(3, QtGui.QFormLayout.FieldRole, self.client_auth_line_edit)

        spacer_item_global_bottom = QtGui.QSpacerItem(40, 80, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.vertical_layout.addItem(spacer_item_global_bottom)

        self.horizontal_layout_bottom = QtGui.QHBoxLayout()
        spacer_item_global_left = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontal_layout_bottom.addItem(spacer_item_global_left)

        self.exit_button = QtGui.QPushButton(self.vertical_layout_widget)
        self.exit_button.setIcon(self.icon.get(config.ICON_CLEAR))
        self.exit_button.setText(tool._from_utf8('Exit'))
        self.exit_button.clicked.connect(self.on_back_clicked)
        self.horizontal_layout_bottom.addWidget(self.exit_button)

        self.connect_test_button = QtGui.QPushButton(self.vertical_layout_widget)
        self.connect_test_button.setIcon(self.icon.get(config.ICON_OFFLINE))
        self.connect_test_button.setText(tool._from_utf8('Test Connect'))
        self.connect_test_button.clicked.connect(self.on_connect_test_clicked)
        self.horizontal_layout_bottom.addWidget(self.connect_test_button)

        self.connect_button = QtGui.QPushButton(self.vertical_layout_widget)
        self.connect_button.setIcon(self.icon.get(config.ICON_OFFLINE))
        self.connect_button.setText(tool._from_utf8('Save Connect'))
        self.connect_button.clicked.connect(self.on_connect_clicked)
        self.horizontal_layout_bottom.addWidget(self.connect_button)

        spacer_item_global_right = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontal_layout_bottom.addItem(spacer_item_global_right)
        self.vertical_layout.addLayout(self.horizontal_layout_bottom)

    def on_back_clicked(self):
        self.close()

    def on_connect_test_clicked(self):
        print('on_connect_test_clicked')

    def on_connect_clicked(self):
        print('on_connect_clicked')

    def set_data(self, data):
        id = data['id']
        name, auth = data['name'], data['auth']
        host, port = data['node']
        self.client_name_line_edit.setText(tool._from_utf8(name))
        self.client_host_line_edit.setText(tool._from_utf8(host))
        self.client_port_line_edit.setText(tool._from_utf8(str(port)))
        self.client_auth_line_edit.setText(tool._from_utf8(auth))

    def closeEvent(self, event):
        self.parent.close()